<?php
/**
 * Section Grid Module
 */


use Fox\Html\Button;
use Fox\Behaviors;
use Fox\Generator;

$xview = Xview::instance();
$surveyid = $xview->val('surveyid');

$grid = new Fox\Grid\Tree('sections');
$grid->source(Generator::url('survey','sections',null,array('surveyid' => $surveyid)));
$grid->fieldcat(array(
				
				array('section_label','label'),
				array('section_description','description'),
				array('section_introduction','introduction'),
				array('section_conclusion','conclusion'),
				'position',
				'sectionid',
				'changed',
				'id',
				'iedit',
				'add',
				'idele',
				
				));
//$grid->fieldcat()->switchToObjectMode();
$grid->fieldcat()->field('position')
			->SetHiddenInput()
			->addClass('position')
			->hidden()
			->width(5);
$grid->fieldcat()->field('sectionid')
			->SetHiddenInput()
			->addClass('sectionid')
			->hidden()
			->width(5);
$grid->fieldcat()->field('id')
		->SetHiddenInput()
		->hidden();
$grid->fieldcat()->field('changed')
			->addClass('changed')
			->SetHiddenInput()
			->hidden();

$grid->fieldcat()->field('label')->width(10);
$grid->fieldcat()->field('description')->width(10);
$grid->fieldcat()->field('introduction')->width(10);
$grid->fieldcat()->field('conclusion')->width(10);

$grid->fieldcat()->setGenerators(array(
	'description' => Generator::useFunction(function($data) {
		if(!empty($data['description'])) {
			return  \Fox\Utilities\Text::reduce($data['description'],50,'...');
		}
		
	}),
	'introduction' => Generator::useFunction(function($data) {
		if(!empty($data['introduction'])) {
			return  \Fox\Utilities\Text::reduce($data['introduction'],50,'...');
		}
	}),
	'conclusion' => Generator::useFunction(function($data) {
		if(!empty($data['conclusion'])) {
			return  \Fox\Utilities\Text::reduce($data['conclusion'],50,'...');
		}
	}),
	'label' => Generator::useFunction(function($data) {
		if(!empty($data['label']['fldvl'])) {
			$data['label']['fldvl'] =  \Fox\Utilities\Text::reduce($data['label']['fldvl'],70,'...');
		}
		return $data['label'];
	}),
	'iedit' => Generator::useValue('?'),
	'add' => Generator::useValue('?'),
	'idele' => Generator::useValue('?'),
	'changed'  => Generator::useValue(false)
));
$grid->fieldcat()->behaviors(Fox\Behaviors::value('draggable'));
$grid->fieldcat()
	->field('iedit')
	->renderer()
	->behaviors(Behaviors::action(
		Generator::useFunction(function($data) use($surveyid) {
			if(!empty($data['questionid'])) {
				$gen = Generator::url('survey','questions','modify',
						array('surveyid' => $surveyid,
							'sectionid' => $data['sectionid'],
							'questionid' => $data['questionid']));
			} else {
				$gen = Generator::url('survey','sections','modify',
						array('surveyid' => $surveyid,
							'sectionid' => $data['sectionid']));
			}
			return Generator::generateValue($gen, $data);
		}),
			true
	));
$grid->fieldcat()
	->field('add')
	->renderer()
	->behaviors(Behaviors::action(
		Generator::useFunction(function($data) use($surveyid) {
			if(!empty($data['questionid'])) {
				$gen = Generator::url('survey','questions','modify',
						array('surveyid' => $surveyid,
							'sectionid' => $data['sectionid'],
							'parentid' => $data['questionid']));
			} else {
				$gen = Generator::url('survey','questions','modify',
						array('surveyid' => $surveyid,
							'sectionid' => $data['sectionid']));
			}
			return Generator::generateValue($gen, $data);
		}),
			true
	));
$grid->fieldcat()
	->field('idele')
	->renderer()
	->behaviors(Behaviors::action(
		Generator::useFunction(function($data) use($surveyid) {
			if(!empty($data['questionid'])) {
				$gen = Generator::url('survey','questions','delete',
				array('inlineid' => $data['questionid']));
			} else {
				$gen = Generator::url('survey','sections','delete',
						array('inlineid' => $surveyid.':'.$data['sectionid']));
			}
			return Generator::generateValue($gen, $data);
		}),
			true
	));
// Add Button
$grid->footer()
	->add(new Button('addSection',null,
						Msg(Nawa\Messages::BUNDLE,Nawa\Messages::SURVEY_ADD_SECTION)))
	->behaviors(Behaviors::action(
		Generator::url('survey','sections','modify',
			array('surveyid' => $surveyid)),true
	));
if($xview->ajax()) {
	$survey = $xview->component('survey','object');
	$grid->initialize($survey->tree());
	echo $grid->display();
} else {
	return $grid;
}
